package com.hyz.assetManager.service.impl;

import clients.AssetClient;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.hyz.assetManager.listener.UploadAssetListener;
import com.hyz.assetManager.pojo.UploadAsset;
import com.hyz.assetManager.service.UploadAssetService;
import com.hyz.pkg.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import pojo.Asset;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HGD
 * @date 2022/9/26 14:29
 */
@Service
public class UploadAssetServiceImpl implements UploadAssetService {

    @Autowired
    private AssetClient client;

    @Override
    public ResponseResult<String> uploadAsset(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), UploadAsset.class, new UploadAssetListener(client)).sheet().doRead();
            return new ResponseResult<>(200, "导入成功", null);
        } catch (Exception e) {
            if (e instanceof ExcelDataConvertException) {
                ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) e;
                String massage = "第" + (excelDataConvertException.getRowIndex() + 1) + "行第"
                        + (excelDataConvertException.getColumnIndex() + 1) + "列的数据出现错误";
                return new ResponseResult<>(400, massage, null);
            }
            return new ResponseResult<>(400, "后台服务正在更新，请稍后再试", null);
        }
    }


    @Override
    public String hello() {
        return client.hello("hyz测试1");
    }
}
